{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "from suite2p import run_s2p\n",
    "\n",
    "# populates ops with the default options\n",
    "ops = run_s2p.default_ops()\n",
    "# specify a folder with sbx files\n",
    "ops['data_path'] = ['e:/data/2photon/raw/191103_JC121_2P_V1_JC/run02_speedup_8dir_whiskers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# update options from a scanbox file (multi-plane experiment; supports only sawtooth mode)\n",
    "from suite2p.io.utils import search_for_ext\n",
    "from suite2p.io.sbx import sbx_get_info\n",
    "# get parameters from the first sbx file\n",
    "fname = search_for_ext(ops['data_path'][0],'sbx')\n",
    "info = sbx_get_info(fname[0])\n",
    "# number of planes\n",
    "nplanes = 1\n",
    "if info.volscan and not isinstance(info.otwave,int):\n",
    "    nplanes = len(info.otwave)\n",
    "# number of channels\n",
    "nchannels = 1\n",
    "if info.channels == 1:\n",
    "    nchannels = 2\n",
    "# aspect ratio and mag\n",
    "magidx = info.config.magnification - 1\n",
    "um_per_pix_x = info.calibration[magidx].x\n",
    "um_per_pix_y = info.calibration[magidx].y\n",
    "mag = info.config.magnification_list[magidx]\n",
    "# frame rate\n",
    "factor = 2 if info.scanmode == 0 else 1\n",
    "fs = factor*(info.resfreq/info.config.lines)/nplanes\n",
    "ops['input_format'] = 'sbx'\n",
    "if info.scanmode == 1:\n",
    "    # do not remove dead columns\n",
    "    ops['sbx_ndeadcols'] = 0\n",
    "else:\n",
    "    # remove dead columns when in bidi mode(trim data)\n",
    "    ops['sbx_ndeadcols'] = -1\n",
    "# set suite2p ops\n",
    "ops['fs'] = fs/nplanes\n",
    "ops['nplanes'] = nplanes\n",
    "ops['nchannels'] = nchannels\n",
    "ops['aspect'] = um_per_pix_x/um_per_pix_y\n",
    "# add some extra info to the ops\n",
    "ops['magnification'] = mag\n",
    "ops['um_per_pixel_x'] = um_per_pix_x\n",
    "ops['um_per_pixel_y'] = um_per_pix_y\n",
    "\n",
    "# run one experiment\n",
    "opsEnd=run_s2p.run_s2p(ops=ops,db={})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
